﻿<?php
/**
 * 分词类
 * ==============================================
 * @auther ChunYang.Jiang<chunyang#mrjiang.com>
 * @date 2012-8-27
 * @copyright blog.mrjiang.com
 * @since 1
 * ==============================================
 */
class Splitword{
	//字典路径
	private $_dict_path;
	//规则库路径
	private $_rule_path;
	//待解析文本
	private $_text;
	//返回记录数，默认0，表示返回全部数据
	private $_top = 0;
	//字符集
	private $_charset = 'utf-8';
	//是否忽略标点符号
	private $_ignore_punctuation = true;
	//是否复式分割，如中国人，返回中国，人，和中国人。
	//取值：1，2，4，8
	private $_multiple = 1;
	//闲散文字是否以二次分词法进行聚合
	private $_duality = false;
	//返回词的词性
	private $_attr = array();
	//返回值
	private $_result = array();
	//分词组件句柄
	private $_handle = null; 
	
	public function __construct($dict_path, $rule_path, $charset='utf-8', $ignore_punctuation='true'){
		$this->_handle = scws_open();
		$this->dict($dict_path);
		$this->rule($rule_path);
		$this->charset($charset);
		$this->ignorePunctuation($ignore_punctuation);
	}
	
	public function attr($attr=array()){
		if ($attr){
			$this->_attr = $attr;
			scws_set_attr($this->_handle, 
				implode(',', $this->_attr)
			);
		}else{
			return $this->_attr;
		}
	}
	
	public function text($text){
		if ($text){
			$this->_text = $text;
			scws_send_text($this->_handle, $this->_text);
		}else{
			return $this->_text;
		}
	}
	
	public function dict($dict=''){
		if($dict){
			$this->_dict_path = $dict;
			scws_set_dict($this->_handle, $this->_dict_path);
		}else{
			return $this->_dict_path;
		}
	}
	
	public function rule($rule=''){
		if($rule){
			$this->_rule_path = $rule;
			scws_set_rule($this->_handle, $this->_rule_path);
		}else{
			return $this->_rule_path;
		}
	}
	
	public function ignorePunctuation($bool){
		$this->_ignore_punctuation = $bool;
		scws_set_ignore($this->_handle, $this->_ignore_punctuation);
	}
	
	public function duality($bool){
		$this->_duality = $bool;
	}
	
	public function charset($charset){
		if ($charset){
			$this->_charset = $charset;
			scws_set_charset($this->_handle, $this->_charset);
		}else{
			return $this->_charset;
		}
	}
	
	public function top($num=-1){
		if ($num>=0){
			$this->_top = $num;
		}else{
			return $this->_top;
		}
	}
	
	
	
	public function getResult($text=''){
		if ($text){
			$this->text($text);
		}
		$this->_result = scws_get_result($this->_handle);
		return $this->_result;
	}
	
	public function clean(){
		scws_close($this->_handle);
	}
	
}